﻿package com.tudou.ad.data
{
    import com.tudou.ad.consts.*;
    import com.tudou.utils.*;

    public class AdData extends Object
    {
        protected var _formerAdShowed:Array;
        protected var _adThrowInfo:AdThrowInfo;
        public var adIds:Array;
        protected var allAdItem:Array;
        protected var _isFormerAdShowed:Boolean = false;
        protected var currentAllItems:Array;
        private static var instance:AdData = null;

        public function AdData()
        {
            _formerAdShowed = [];
            adIds = [];
            allAdItem = [];
            currentAllItems = [];
            _adThrowInfo = new AdThrowInfo();
            return;
        }// end function

        public function removeAdItemByType(param1:int) : void
        {
            delete allAdItem[param1];
            return;
        }// end function

        public function get adThrowInfo() : AdThrowInfo
        {
            return _adThrowInfo;
        }// end function

        public function reset() : void
        {
            allAdItem = [];
            currentAllItems = [];
            adIds = [];
            return;
        }// end function

        public function getAdItemByType(param1:int) : AdItemInfo
        {
            var _loc_2:AdItemInfo = null;
            var _loc_3:AdItemInfo = null;
            var _loc_4:* = undefined;
            var _loc_5:* = undefined;
            var _loc_6:Array = null;
            if (param1 == AdType.BACKGROUND)
            {
                _loc_3 = allAdItem[AdType.BACKGROUND];
                if (_loc_3 != null && _formerAdShowed.indexOf(_loc_3.aid) !== -1)
                {
                    if (!_isFormerAdShowed)
                    {
                        allAdItem[AdType.BACKGROUND] = null;
                        delete allAdItem[AdType.BACKGROUND];
                        _loc_3 = null;
                    }
                }
                _loc_2 = _loc_3;
            }
            else if (param1 == AdType.FRONT || param1 == AdType.PLACARD || param1 == AdType.FRONT_LAST)
            {
                _loc_4 = allAdItem[param1];
                if (_loc_4 as AdItemInfo)
                {
                    _loc_2 = _loc_4;
                    allAdItem[param1] = null;
                    delete allAdItem[param1];
                }
                else if (_loc_4 as Array && _loc_4.length > 0)
                {
                    _loc_2 = (allAdItem[param1] as Array).shift();
                }
                else
                {
                    _loc_2 = null;
                }
            }
            else
            {
                _loc_5 = allAdItem[param1];
                if (_loc_5 as AdItemInfo)
                {
                    _loc_2 = _loc_5;
                }
                else if (_loc_5 as Array && _loc_5.length > 0)
                {
                    _loc_6 = _loc_5;
                    _loc_2 = _loc_6[MathUtil.rangeRand(0, (_loc_6.length - 1))];
                }
                else
                {
                    _loc_2 = null;
                }
            }
            currentAllItems[param1] = _loc_2;
            return _loc_2;
        }// end function

        public function removeAdItemByTid(param1:int) : void
        {
            var _loc_2:Array = null;
            var _loc_3:AdItemInfo = null;
            var _loc_4:String = null;
            var _loc_5:int = 0;
            for (_loc_4 in allAdItem)
            {
                
                _loc_2 = allAdItem[_loc_4] as Array;
                _loc_3 = allAdItem[_loc_4] as AdItemInfo;
                if (_loc_2)
                {
                    _loc_5 = 0;
                    while (_loc_5 < _loc_2.length)
                    {
                        
                        _loc_3 = _loc_2[_loc_5];
                        if (param1 == _loc_3.aid)
                        {
                            _loc_2.splice(_loc_5, 1);
                        }
                        _loc_5++;
                    }
                    continue;
                }
                if (_loc_3)
                {
                    if (param1 == _loc_3.aid)
                    {
                        delete allAdItem[_loc_4];
                    }
                }
            }
            return;
        }// end function

        private function _fetAdItemInfos(param1:int) : Array
        {
            var _loc_2:* = allAdItem[param1];
            if (_loc_2 as AdItemInfo)
            {
                return [_loc_2];
            }
            if (_loc_2 as Array && _loc_2.length > 0)
            {
                return _loc_2;
            }
            return null;
        }// end function

        public function set adThrowInfo(param1:AdThrowInfo) : void
        {
            _adThrowInfo = param1;
            return;
        }// end function

        public function getFromerAds() : Array
        {
            var _loc_1:* = _fetAdItemInfos(AdType.PLACARD);
            var _loc_2:* = _fetAdItemInfos(AdType.FRONT);
            var _loc_3:* = _fetAdItemInfos(AdType.FRONT_LAST);
            var _loc_4:Array = [];
            if (_loc_1 != null)
            {
                _loc_4 = _loc_4.concat(_loc_1);
            }
            if (_loc_2 != null)
            {
                _loc_4 = _loc_4.concat(_loc_2);
            }
            if (_loc_3 != null)
            {
                _loc_4 = _loc_4.concat(_loc_3);
            }
            if (_loc_4.length == 0)
            {
                _loc_4 = null;
            }
            return _loc_4;
        }// end function

        public function initFormerAdShowed() : void
        {
            _formerAdShowed = [];
            return;
        }// end function

        public function setAdItemByType(param1:Object, param2:int) : void
        {
            allAdItem[param2] = param1;
            return;
        }// end function

        public function getCurrAdItemByType(param1:int) : AdItemInfo
        {
            return currentAllItems[param1];
        }// end function

        public function addFormerAdShowed(param1:int) : void
        {
            _formerAdShowed.push(param1);
            return;
        }// end function

        public function set isFormerAdShowed(param1:Boolean) : void
        {
            _isFormerAdShowed = param1;
            return;
        }// end function

        public function get isFormerAdShowed() : Boolean
        {
            return _isFormerAdShowed;
        }// end function

        public static function getInst() : AdData
        {
            var _loc_1:* = instance || new AdData;
            instance = instance || new AdData;
            return _loc_1;
        }// end function

    }
}
